<?php
/**
 * 视图抽象类 Yjnic_View
 *
 * @author prettyyjnic <prettyyjnic@qq.com> 20151027
 */
abstract class Yjnic_ViewAbstract
{
	protected $_config = array();
	protected $_args = array();

	public function assign($assign, $value=''){}
	public function display($template_name){}

}

/**
 * 视图类 Yjnic_View
 *
 * @author prettyyjnic <prettyyjnic@qq.com> 20151027
 */
class Yjnic_View extends Yjnic_ViewAbstract {

	public function __construct($config=array()){
		$this->_config = $config;		
	}

	/**
	 * 赋值模板
	 * @param $name
	 * @param $value
	 */
	public function assign($name, $value = ''){
		$this->_args[$name] = $value;
	}

	/**
	 * 获取要输出的模板信息
	 */
	public function fetch($template_name){
		$this_template_path = $this->_config['path'].'/'.$template_name;
		if(!is_file($this_template_path)){
			throw new Yjnic_Exception('模板文件不存在', 501);
		}
		extract($this->_args);
		
		ob_start();
		ob_implicit_flush(0);
		include $this_template_path;
		$tpl_content = ob_get_clean();
		return $tpl_content;
	}

	/**
	 * 模板输出
	 * @param $templateFile
	 * @param $cache_id
	 * @parma $return_string
	 */
	public function display($template_name){
		header("Content-type: text/html; charset=utf-8");
		$cache_control = isset($this->_config['cache_control']) ? $this->_config['cache_control'] : 'private';
		header('Cache-control: '.$cache_control);  // 页面缓存控制
        header('X-Powered-By:Prettyyjnic');
		echo($this->fetch($template_name));
	}

}